package com.du.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author : DuJiabao
 * @Date : 2021/3/5 22:50
 * @Project : MyBatis
 * @File : null.java
 * @Desc :
 */
public class Utils {
  private static SqlSessionFactory sqlSessionFactory;
  static String resource = "mybatis-config.xml";  // mybatis的配置xml文件

  static {
    try {
      InputStream inputStream = Resources.getResourceAsStream(resource);  // 获取文件输入流
      sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取SqlSession的工厂
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static SqlSession getSqlSession() {
    return sqlSessionFactory.openSession();  // 返回一个SqlSession
  }
}
